import axios from "axios";
import nProgress from "nprogress";
import "nprogress/nprogress.css";

//创建axios的实例
const request = axios.create({
  //请求地址前缀
  baseURL: process.env.VUE_APP_MOCK_API,
  //请求时间
  timeout: 10000,
});

//请求拦截器
request.interceptors.request.use(
  //axios的配置信息
  (config) => {
   
    nProgress.start();
    return config;
  },
  //catch方法
  (error) => {
    nProgress.done();
    return Promise.reject(error.message);
  }
);

//响应拦截器
request.interceptors.response.use(
  //axios的响应信息
  (response) => {
    nProgress.done();
    //对响应的状态做出判断
    if (response.status === 200) {
      return response.data.data;
    } else {
      nProgress.done();
      return Promise.reject({ massage: response.data.message });
    }
  },
  //catch方法
  (error) => {
    nProgress.done();
    return Promise.reject(error);
  }
);

export default request;
